package com.weishu.installment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.weishu.installment.domain.TJrProduct;
import com.weishu.installment.domain.TJrRentInstallment;
import com.weishu.installment.service.TJrProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("product")
public class JrProductController {
    @Resource
    TJrProductService productService;

    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping("list")
    @RequiresPermissions("product:product:list")
    public List<TJrProduct> list(){
        List<TJrProduct> productlist = (List<TJrProduct>) redisTemplate.opsForValue().get("productlist");
        if (!redisTemplate.hasKey("productlist")){
            List<TJrProduct> list = productService.list();
            redisTemplate.opsForValue().set("productlist",list,24, TimeUnit.HOURS);
            return list;
        }
        return productlist;
    }

    @GetMapping("productByid")
    @RequiresPermissions("product:product:productByid")
    public TJrProduct productByid(String id){
        TJrProduct byId =
                productService.getById(id);
        System.err.println(byId);
        return byId;
    }
}
